package com.l.service;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.l.dao.ExcelDao;
import com.l.entity.User;
import com.l.utils.ExcelUtils;

@Service
public class ExcelService {
	@Autowired
	ExcelDao excelDao;
	public String insertUser(MultipartFile file){
		if (file.isEmpty()) {
			try {
				throw new Exception("文件不存在！");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		InputStream in = null;
		try {
			in = file.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}

		List<List<Object>> listob = null;
		try {
			listob = new ExcelUtils().getBankListByExcel(in,
					file.getOriginalFilename());
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
		for (int i = 0; i < listob.size(); i++) {
			List<Object> lo = listob.get(i);
			User vo = new User();
			vo.setName(String.valueOf(lo.get(0)));
			BigDecimal b=new BigDecimal(Double.parseDouble(String.valueOf(lo.get(1))));
			double f1 = b.setScale(2,RoundingMode.HALF_UP).doubleValue();
			vo.setBalance(String.valueOf(f1));
			excelDao.insertUser(vo);
		}

		return "success";
	}
	public List<User> selectUser(){
		return excelDao.selectUser();
	}
}
